package com.renwu.edu.info.manager.controller;

import com.renwu.edu.info.manager.domain.Student;
import com.renwu.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        lo:
        while (true) {
            System.out.println("--------------------欢迎来到学生系统--------------------");
            System.out.print("1.添加学生\t");
            System.out.print("2.删除学生\t");
            System.out.print("3.修改学生\t");
            System.out.print("4.查询学生\t");
            System.out.println("5.退出");
            System.out.println("请输入你的选择");
            int chose = sc.nextInt();
            switch (chose) {
                case 1:
                    //System.out.println("添加");
                    AddStudent();
                    break;
                case 2:
                    //System.out.println("删除");
                    deleteStudent();
                    break;
                case 3:
                    //System.out.println("修改");
                    setStudent();
                    break;
                case 4:
                    //System.out.println("查询");
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("退出学生管理系统");
                    break lo;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }

    public void setStudent() {
        int setId = inputIntId();
        Student newStu = inputStudentInfo(setId);
        studentService.setStudent(setId,newStu);
        System.out.println("修改成功");
    }

    public void deleteStudent() {
        int deleteId = inputIntId();
        studentService.deleteStudent(deleteId);
        System.out.println("删除成功");
    }

    public void AddStudent() {
        int id;
        while (true) {
            System.out.println("请输入学生学号");
            id = sc.nextInt();
            boolean flag = studentService.isExists(id);
            if (flag) {
                System.out.println("该学号已经存在，请重新输入");
            } else {
                break;
            }
        }
       Student stu = inputStudentInfo(id);
        boolean result = studentService.AddStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public void findAllStudent() {
        Student[] stu = studentService.findAllStudent();
        if (stu == null) {
            System.out.println("无学生信息，请先添加");
            return;
        }
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null) {
                System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
                System.out.println(stu[i].getId() + "\t\t" + stu[i].getName() + "\t\t" + stu[i].getAge() + "\t\t" + stu[i].getBirthday());
            }
        }
    }

    public int inputIntId(){
        int id;
        while (true) {
            System.out.println("请输入学生学号");
            id = sc.nextInt();
            boolean result = studentService.isExists(id);
            if (result) {
                break;
            } else {
                System.out.println("学号不存在,请重新输入");
            }
        }
        return id;
    }


    public Student inputStudentInfo(int id){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
